package com.edge.pbx.view.eq;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.edge.pbx.view.PbxCircleSeekBar;

import com.edge.pbxdsp.R;
import com.hanlink.dsp.ui.widget.XoverEditText;
import com.hanlink.dsp.util.FreqUtil;
import com.qztech.sdk.model.SettingConstants;
import com.qztech.utils.YzDebug;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class EqContentLayout extends ViewGroup {

    private XoverEditText mEditTextQ, mEditTextFreq;
    public EqContentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
        View view = null;

        final int topHeight = (int) (height*0.21f);
        final int bottomHeight = (int) (height*0.12f);
        final int centerContentHeight = height - topHeight - bottomHeight;
        int w,h;
        for (int i = 0, len = getChildCount(); i < len; i++){
            view = getChildAt(i);
            if (view == null)continue;
            switch (view.getId()){
                case R.id.esv_eqchart:
                    measuredChild(view,0.8f *width, topHeight);
                    break;
                case R.id.vpChannels:
                    measuredChild(view, width/2, centerContentHeight*0.85f);
                    break;
                case R.id.idv_eq_layout:
                    measuredChild(view, width/2 , centerContentHeight*0.15f);
                    break;
                case R.id.eq_seekbar_q:
                case R.id.eq_seekbar_freq:
                    float size = Math.min(width*0.2f, centerContentHeight*0.45f);
                    measuredChild(view, size, size);
                    break;
                case R.id.pbx_eq_output_select_layout_wrapper:
                    size = Math.min(width*0.3f, centerContentHeight*0.9f);

                    measuredChild(view, size, size);
                    break;
                case R.id.eq_bottom_btn_layout:
                    measuredChild(view, width, bottomHeight);
                    break;
                case R.id.pbx_eq_et_q:
                    mEditTextQ = (XoverEditText) view;
                    break;
                case R.id.pbx_eq_et_freq:
                    mEditTextFreq = (XoverEditText) view;
                    break;
            }
        }



    }
    private void measuredChild(View view, float w, float h){
        view.measure(MeasureSpec.makeMeasureSpec((int) w,MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec((int) h,MeasureSpec.EXACTLY));

    }


    private void layoutEditText(View view, int l, int t, int pw, int ph){
        if (view == null)return;
        int w = (int) (pw * 0.6);
        int h = (int) (ph * 0.18f);
        int viewTop = (int) ((ph *  0.55f) - h/2);
        l = l + (pw - w)/2;
        t = t + viewTop;
        view.layout(l, t, l+w, t +h );
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        final int height = b - t;
        final int topHeight = (int) (height*0.21f);
        final int bottomHeight = (int) (height*0.12f);
        final int centerContentHeight = height - topHeight - bottomHeight;
        int w,h;
        View view = null;
        int mRightUseHeight = findViewById(R.id.eq_seekbar_freq).getMeasuredWidth()
                +findViewById(R.id.pbx_eq_output_select_layout_wrapper).getMeasuredWidth();
        int rightSpace = (width/2 - mRightUseHeight)/3;
        YzDebug.d("Yang", " ------->3:" + rightSpace +" " + width/2 +" " + mRightUseHeight);
        for (int i = 0, len = getChildCount(); i < len; i++){
            view = getChildAt(i);
            if (view == null)continue;
            switch (view.getId()){
                case R.id.esv_eqchart:
                    l = (width - view.getMeasuredWidth()) >> 1;
                    view.layout(l , 0, width-l, view.getMeasuredHeight());
                    break;
                case R.id.vpChannels:
                    view.layout(0,topHeight,view.getMeasuredWidth(),topHeight+view.getMeasuredHeight());
                    break;
                case R.id.idv_eq_layout:
                    w = view.getMeasuredWidth();
                    h = view.getMeasuredHeight();
                    b = height - bottomHeight;
                    view.layout(0, b - h, w, b);
                    break;
                case R.id.eq_seekbar_q:
                    w = view.getMeasuredWidth();
                    h = view.getMeasuredHeight();
                    l = width/2 + rightSpace;
                    t = topHeight + centerContentHeight/4 - h/2;
                    view.layout(l, t , l + w, t+h);
                    layoutEditText(mEditTextQ, l, t, w, h);
                    break;
                case R.id.eq_seekbar_freq:
                    w = view.getMeasuredWidth();
                    h = view.getMeasuredHeight();
                    l = width/2 + rightSpace;
                    t = topHeight + centerContentHeight*3/4 - h/2;
                    view.layout(l, t , l + w, t+h);
                    layoutEditText(mEditTextFreq, l, t, w, h);
                    l += w;
                    break;
                case R.id.pbx_eq_output_select_layout_wrapper:
                    w = view.getMeasuredWidth();
                    h = view.getMeasuredHeight();
                    t = topHeight + (centerContentHeight-h)/2;
                    l = l + rightSpace;
                    view.layout(l, t, l+w, t +h);
                    break;
                case R.id.eq_bottom_btn_layout:
                    view.layout(0, height-bottomHeight, width, height);
                    break;
            }
        }

    }
}
